package interfaces.login;

import java.util.List;

import org.fenggui.ComboBox;
import org.fenggui.TextEditor;
import org.fenggui.binding.render.ImageFont;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.util.Color;
import org.fenggui.util.Dimension;

import main.InitGame;
import fileHandling.ProfileHandler;
import fileHandling.language.interfaces.LoginText;
import fileHandling.language.interfaces.MenuText;
import gameStates.absGamesStates.gui.AbsLoginState;
import interfaces.GUISource;
import interfaces.language.LanguageButton;
import interfaces.language.LanguageLabel;
import interfaces.options.CheckBoxOptionWidget;
import interfaces.superWidgets.InterfaceElement;
import interfaces.superWidgets.StaticContent;
import interfaces.superWidgets.tabs.TabSwitchContent;

public class LoginElement extends InterfaceElement {
	
	private TabSwitchContent tabSwitchContent;
	private StaticContent loginContent, registerContent;
	private LanguageButton loginButton, registerButton;
	private CheckBoxOptionWidget autoLoginBox;
	
	private ImageFont font;
	private Dimension contentSize;
	private ComboBox profileSelection;
	private TextEditor profileEditor;
	
	public LoginElement(AbsLoginState loginState) {
		super(loginState);
		
		font = GUISource.middleFont;
		
		int width = getWidth() / 2;
		int height = TabSwitchContent.getTabButtonHeight(font) * 3;
		tabSwitchContent = new TabSwitchContent(width, height, font, width / 3);
		tabSwitchContent.setXY((getWidth() / 2) - (width / 2), (getHeight() / 2) - (height / 2));
		addWidget(tabSwitchContent);
		
		contentSize = tabSwitchContent.getSwitchContentSize();
		
		createLoginContent();
		createRegisterContent();
		
		createButtons();
		
		if(ProfileHandler.getProfiles().isEmpty()) {
			tabSwitchContent.switchContent(registerContent);
			enableLoginButton(false);
			loginState.getGUIDisplay().setFocusedWidget(profileEditor);
		} else tabSwitchContent.switchContent(loginContent);
	}
	
	private void enableLoginButton(boolean enable) {
		loginButton.setEnabled(enable);
		loginButton.getAppearance().removeAll();
		
		Color color = enable ? Color.GRAY : new Color(0.3f, 0.3f, 0.3f);
		loginButton.getAppearance().add(new PlainBackground(color));
		
		Color textColor = enable ? Color.WHITE : Color.GRAY;
		GUISource.setTextColor(loginButton, textColor);
		loginButton.layout();
	}
	
	private void createLoginContent() {
		loginContent = new StaticContent(contentSize);
		
		int widgetWidth = contentSize.getWidth();
		int widgetHeight = contentSize.getHeight() / 3;
		int widgetX = 0;
		
		profileSelection = GUISource.createComboBox(GUISource.middleFont);
		profileSelection.setSize(widgetWidth, widgetHeight);
		profileSelection.setXY(widgetX, contentSize.getHeight() - widgetHeight);
		loginContent.addWidget(profileSelection);
		
		List<String> profiles = ProfileHandler.getProfiles();
		for(String profile : profiles) {
			profileSelection.addItem(profile);
		}
		
		int autoWidth = widgetWidth / 2;
		LanguageLabel autoLabel = new LanguageLabel(LoginText.Auto_Login, font);
		autoLabel.setSize(autoWidth, widgetHeight);
		autoLabel.setXY(widgetX, widgetHeight);
		loginContent.addWidget(autoLabel);
		
		autoLoginBox = new CheckBoxOptionWidget("false", LoginText.Auto_Login);
		autoLoginBox.setSize(autoWidth, widgetHeight);
		autoLoginBox.setXY(widgetX + autoWidth, widgetHeight);
		autoLoginBox.changeValue("" + ProfileHandler.isAutoLogin());
		loginContent.addWidget(autoLoginBox);
		
		LanguageButton button = new LanguageButton(LoginText.Login, font);
		button.setSize(widgetWidth, widgetHeight);
		button.setX(widgetX);
		button.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				String selectedProfile = profileSelection.getSelectedValue();
				if(selectedProfile != null && ProfileHandler.getProfiles().contains(selectedProfile)) 
					login(selectedProfile);
			}
		});
		loginContent.addWidget(button);
	}
	
	private void createRegisterContent() {
		registerContent = new StaticContent(contentSize);
		int widgetWidth = contentSize.getWidth();
		int widgetHeight = contentSize.getHeight() / 2;
		int widgetX = 0;
		
		profileEditor = new TextEditor();
		GUISource.setTheme(profileEditor);
		GUISource.setFont(profileEditor, font);
		profileEditor.setSize(widgetWidth, widgetHeight);
		profileEditor.setXY(widgetX, contentSize.getHeight() - widgetHeight);
		registerContent.addWidget(profileEditor);
		
		LanguageButton button = new LanguageButton(LoginText.Create_New_Profile, font);
		button.setSize(widgetWidth, widgetHeight);
		button.setX(widgetX);
		button.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				String text = profileEditor.getText();
				if(text != null && !Character.isWhitespace(text.charAt(0))) {
					text = text.substring(0, text.length() - 1);
					ProfileHandler.createNewProfileFiles(text);
					profileSelection.addItem(text);
					profileSelection.setSelected(text);
					enableLoginButton(true);
					profileEditor.setText("");
					tabSwitchContent.switchContent(loginContent);
				}
			}
		});
		registerContent.addWidget(button);
	}
	
	private void createButtons() {
		loginButton = new LanguageButton(LoginText.Login, font);
		tabSwitchContent.addSwitchContent(loginButton, loginContent);
		
		registerButton = new LanguageButton(LoginText.Create_New_Profile, font);
		tabSwitchContent.addSwitchContent(registerButton, registerContent);
		
		LanguageButton quitButton = new LanguageButton(MenuText.Quit, font);
		quitButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				InitGame.get().exit();
			}
		});
		tabSwitchContent.addTabButton(quitButton);
	}
	
	public void login(String profileName) {
		InitGame.get().login(profileName, Boolean.valueOf(autoLoginBox.getValue()).booleanValue());
	}
	
	@Override
	public void updateMoney(int newMoney) {}
}